//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.entity.President;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.EmployeeService;
import com.example.service.PresidentService;
import com.example.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebController {
    @Resource
    private StudentService studentService;
    @Resource
    private PresidentService presidentService;
    @Resource
    private AdminService adminService;

    public WebController() {
    }

    @PostMapping({"/login"})
    public Result login(@RequestBody Account account) {
        if (account.getRole() == null || (!"ADM".equals(account.getRole()) && !"STU".equals(account.getRole()) && !"PRE".equals(account.getRole()))) {
            return Result.error("400", "无效的用户角色");
        }

        Account result = null;
        if ("ADM".equals(account.getRole())) {
            result = this.adminService.login(account);
        } else if ("STU".equals(account.getRole())) {
            result = this.studentService.login(account);
        } else if ("PRE".equals(account.getRole())) {
            result = this.presidentService.login(account);
        }

        if (result == null) {
            return Result.error("500", "用户角色错误，无权限登录");
        }
        return Result.success(result);
    }

    @PostMapping({"/register"})
    public Result register(@RequestBody Account account) {
        Student dbstudent = studentService.selectByUsername(account.getUsername());
        if (dbstudent == null) {
            Student student = new Student(account);
         this.studentService.register(student);
        } else {
            return Result.error();
        }
        return Result.success();
    }

    @PutMapping({"/updatePassword"})
    public Result updatePassword(@RequestBody Account account) {
        if ("ADM".equals(account.getRole())) {
            this.adminService.updatePassword(account);
        } else if ("STU".equals(account.getRole())) {
                this.studentService.updatePassword(account);
            } else if("PRE".equals(account.getRole())) {
                    this.presidentService.updatePassword(account);
                    } else{
                        throw new CustomException("500", "不存在该用户");
                    }
        return Result.success();
    }
}
